
//
//  HTPaymentMainSendCodeView.m
//  HTPaymentFramework
//
//  Created by 内蒙小猿 on 2018/4/13.
//  Copyright © 2018年 zhaozhongpeng. All rights reserved.
//

#import "HTPaymentMainSendCodeView.h"
#import "HTPaymentInputSendCode.h"
#import "HTPaymentCountDownButton.h"
#import "PayHeaderFile.h"

@interface HTPaymentMainSendCodeView()

@property (nonatomic,strong) UILabel *topAlertLable;

@property (nonatomic,strong) HTPaymentInputSendCode *HTPaymentInputSendCode;

@property (nonatomic,strong) HTPaymentCountDownButton *sendCodeBtn;


@end

@implementation HTPaymentMainSendCodeView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}



- (void)createUI{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topAlertLable];
    [self addSubview:self.HTPaymentInputSendCode];
    [self addSubview:self.sendCodeBtn];
    
    
    //
    NSLayoutConstraint *topAlertLableTop = [NSLayoutConstraint constraintWithItem:self.topAlertLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:HTPaymentRealHeight(10)];
    
    NSLayoutConstraint * topAlertLableLeft = [NSLayoutConstraint constraintWithItem:self.topAlertLable attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    
    NSLayoutConstraint * topAlertLableRight = [NSLayoutConstraint constraintWithItem:self.topAlertLable attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    
    NSLayoutConstraint *topAlertLableHeight = [NSLayoutConstraint constraintWithItem:self.topAlertLable attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:HTPaymentRealHeight(13)];
    
    [self addConstraints:@[topAlertLableTop,topAlertLableLeft,topAlertLableRight,topAlertLableHeight]];
    
    //
    NSLayoutConstraint *HTPaymentInputSendCodeTop = [NSLayoutConstraint constraintWithItem:self.HTPaymentInputSendCode attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topAlertLable attribute:NSLayoutAttributeBottom multiplier:1.0 constant:HTPaymentRealHeight(10)];

    NSLayoutConstraint * HTPaymentInputSendCodeLeft = [NSLayoutConstraint constraintWithItem:self.HTPaymentInputSendCode attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];

    NSLayoutConstraint * HTPaymentInputSendCodeRight = [NSLayoutConstraint constraintWithItem:self.HTPaymentInputSendCode attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];

    NSLayoutConstraint * HTPaymentInputSendCodeHeight = [NSLayoutConstraint constraintWithItem:self.HTPaymentInputSendCode attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:HTPaymentRealHeight(50)];

    [self addConstraints:@[HTPaymentInputSendCodeTop,HTPaymentInputSendCodeLeft,HTPaymentInputSendCodeRight,HTPaymentInputSendCodeHeight]];
    
    NSLayoutConstraint *sendCodeBtnX = [NSLayoutConstraint constraintWithItem:self.sendCodeBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    
    NSLayoutConstraint * sendCodeBtnY = [NSLayoutConstraint constraintWithItem:self.sendCodeBtn attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.HTPaymentInputSendCode attribute:NSLayoutAttributeBottom multiplier:1.0 constant:HTPaymentRealHeight(30)];
    
    
    [self addConstraints:@[sendCodeBtnX,sendCodeBtnY]];
    
}

- (UILabel *)topAlertLable{
    if (_topAlertLable == nil) {
        _topAlertLable = [[UILabel alloc] init];
        _topAlertLable.font = HTPaymentRealFont(12);
        _topAlertLable.textColor = [UIColor lightGrayColor];
        _topAlertLable.textAlignment = NSTextAlignmentCenter;
        _topAlertLable.text = @"校验码已经发送至尾号1101的手机";
        _topAlertLable.translatesAutoresizingMaskIntoConstraints = NO;
        _topAlertLable.backgroundColor = [UIColor whiteColor];

    }
    return _topAlertLable;
}

- (HTPaymentInputSendCode *)HTPaymentInputSendCode{
    if (_HTPaymentInputSendCode == nil) {
        _HTPaymentInputSendCode = [[HTPaymentInputSendCode alloc] init];
    }
    return _HTPaymentInputSendCode;
}


- (HTPaymentCountDownButton *)sendCodeBtn{
    if (_sendCodeBtn == nil) {
        _sendCodeBtn = [HTPaymentCountDownButton buttonWithType:UIButtonTypeCustom];
        [_sendCodeBtn setTitleColor:[UIColor colorWithRed:69/255.0 green:211/255.0 blue:209/255.0 alpha:1] forState:UIControlStateNormal];
        _sendCodeBtn.translatesAutoresizingMaskIntoConstraints = NO;
        NSString *titleStr = @"重新发送";
        UIColor *titleColor = [UIColor colorWithRed:69/255.0 green:211/255.0 blue:209/255.0 alpha:1];
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:titleStr attributes:@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:HTPaymentRealFont(15),NSUnderlineStyleAttributeName:@(1),NSUnderlineColorAttributeName:titleColor}];
        [_sendCodeBtn setAttributedTitle:att forState:UIControlStateNormal];
        [_sendCodeBtn HTPaymentCountDownButtonHandler:^(HTPaymentCountDownButton *HTPaymentCountDownButton, NSInteger tag) {
            [self sendCodeAction:HTPaymentCountDownButton];
        }];
    }
    return _sendCodeBtn;
}


- (void)sendCodeAction:(HTPaymentCountDownButton *)sender{
    self.sendCodeBtn.enabled = NO;
    [self.sendCodeBtn startCountDownWithSecond:60];
    [self.sendCodeBtn countDownChanging:^NSString *(HTPaymentCountDownButton *HTPaymentCountDownButton,NSUInteger second) {
        NSString *title = [NSString stringWithFormat:@"%zd秒后重发",second];
        return title;
    }];

    [self.sendCodeBtn countDownFinished:^NSString *(HTPaymentCountDownButton *HTPaymentCountDownButton, NSUInteger second) {
        HTPaymentCountDownButton.enabled = YES;
        return @"重新发送";
    }];
}


@end
